<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="../../res/layui/css/layui.css" media="all">
    <link rel="stylesheet"
          href="../../res/zTree_v3-master/css/metroStyle/metroGreen.css"
          type="text/css">
    <script src="../../res/js/jquery-3.4.1.min.js" type="text/javascript"
            charset="utf-8"></script>
    <script type="text/javascript" src="../../res/layui/layui.js"></script>
    <script type="text/javascript" src="../../res/module/common.js"></script>
    <script type="text/javascript" src="../../res/module/treeSelect/treeSelect.js"></script>
    <title>操作界面</title>
</head>
<body>
<div style="margin-top: 10px; padding-right: 27px;">
    <form class="layui-form" id="idform" action="">
        <input id="resultId" type="hidden" name="resultId" lay-verify="title"
               autocomplete="off" class="layui-input" >
        <div class="layui-form-item">
            <label class="layui-form-label">比赛名称</label>
            <div class="layui-input-block">
                <input id="matchName" type="text" name="matchName" lay-verify="required|title"
                       autocomplete="off" placeholder="比赛名称" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">比赛时间</label>
            <div class="layui-input-block">
                <input id="matchTime"  type="datetime" name="matchTime" lay-verify="required|title"
                       autocomplete="off" placeholder="请输入" class="layui-input" style="height:40px;width:170px;">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">比赛地点</label>
            <div class="layui-input-block">
                <input id="matchAddr" type="text" name="matchAddr" lay-verify="required|title"
                       autocomplete="off" placeholder="请输入" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">比赛项目</label>
            <div class="layui-input-block">
                <input name="matchItem" type="text" id="matchItem" placeholder="请输入"
                       lay-filter="tree" class="layui-input" lay-verify="required|title">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">比赛成绩</label>
            <div class="layui-input-block">
                <input name="matchResult" type="text" id="matchResult" placeholder="请输入"
                       lay-filter="tree" class="layui-input" lay-verify="required|title">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">比赛排名</label>
            <div class="layui-input-block">
                <input name="matchSort" type="text" id="matchSort" placeholder="请输入"
                       lay-filter="tree" class="layui-input" lay-verify="required|title">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">比赛积分</label>
            <div class="layui-input-block">
                <input name="score" type="text" id="score" placeholder="请输入"
                       lay-filter="tree" class="layui-input" lay-verify="required|title">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"> 对手姓名：</label>
            <div class="layui-input-block">
                <select name="rivId" id="rivId">
                    <option>请选择</option>
                </select>
            </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="submit" class="layui-btn" lay-submit
                        lay-filter="demo1">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
        </div>
    </form>
<script type="text/javascript">
    //全局同步
    $.ajaxSetup({
        async: false
    });

</script>
<script>
    var form =null;

    layui.use('form', function() {
        form = layui.form;

        var id = getQueryStringOne("resultId");
        var id2 = getQueryStringOne("rivId");
        layui.use('laydate', function(){
            var laydate = layui.laydate;
            laydate.render({
                elem: '#matchTime'
                ,type: 'datetime'
            });
        });
        if(id != "" && id !=null){
            rivId();
            updateQuery();
        }else{
            rivId();
            if(id2  != ""&& id2 !=null){
                $("#rivId").val(id2);
            }
            rivId();
        }
        rivId();
        //监听提交
        form.on('submit(demo1)', function(data) {
            //layer.msg(JSON.stringify(data.field));//弹出选择的信息
            var form = new FormData(document.getElementById("idform"));
            $.ajax({
                url : "../../../atsRivalResult/insertAndUpdate",
                type : "post",
                data : form,
                dataType : "json",
                processData : false,
                contentType : false,
                success : function(obj) {
                    if (obj.num == 1) {
                        //弹出需求
                        layer.msg(obj.msg, {
                            icon : 1,
                            time : 1000
                            //一秒关闭 如果不配置是3秒
                        }, function() {
                            //关闭 层
                            var index = parent.layer
                                .getFrameIndex(window.name);//得到iframe 层的索引
                            parent.layer.close(index);//关闭改层
                            var index = parent.layer
                                .getFrameIndex(window.name);//得到iframe 层的索引
                            parent.layer.close(index);//关闭改层
                            //关闭父级页面的表格
                            parent.layui.table.reload('testReloadTwo');
                        });
                    } else {
                        layer.msg(obj.msg);
                    }
                },
                error : function(e) {
                }
            });
            return false;
        });
    });

    function rivId() {
        var form = layui.form;
        $.post("../../../atsRival/querAll","",function (obj) {
            $.each(obj,function(index,item){
                $("#rivId").append("<option value='"+item.rivId+"'>"+item.rivName+"</option>");
                form.render();
            });
        },"json");
    }
    function updateQuery(){
        //修改是出现的值
        var one = getQueryStringOne("resultId");
        if(one != ""){
            var url = "../../../atsRivalResult/updateQuery";
            var data ={resultId:getQueryStringOne("resultId")};
            $.post(url,data,function(obj){
                $("#resultId").val(obj.resultId);
                $("#matchName").val(obj.matchName);
                $("#matchTime").val(obj.matchTime);
                $("#matchAddr").val(obj.matchAddr);
                $("#matchItem").val(obj.matchItem);
                $("#matchResult").val(obj.matchResult);
                $("#matchSort").val(obj.matchSort);
                $("#score").val(obj.score);
                $("#rivId").val(obj.rivId);
            },'json')
        }
    }
    //得到地址栏上的参数  可以 是中文
    function getQueryStringOne(key) {
        var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null)
            return decodeURI(r[2]);
        return null;
    }
</script>
</body>
</html>